//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================
#include <libpub.h>
#include <bo_cmds.h>

//====================================
//                  In Order To Avoid Header Collisions
typedef enum {
    baud_1200,
    baud_2400,
    baud_4800,
    baud_9600,
    baud_19200,
    baud_38400,
    baud_57600,
    baud_115200,
    baud_230400,
    baud_COUNT
} serial_baud_t;

extern "C" {
int SerialInit(serial_baud_t baudrate);
void init_monahans_nand_driver(void);
void wls_keypad_init(void);
}

//===============================

int Do_InitSerial(void)
{
    SerialInit(baud_115200);
    return 0;
}

int Do_InitKeypad(void)
{
    wls_keypad_init();
    return 0;
}

int Do_InitNand(void)
{
    init_monahans_nand_driver();
    return 0;
}

DECL_INITCMD(Do_InitKeypad, "Init Keypad");
DECL_INITCMD(Do_InitNand, "Init  Nand ");
//DECL_INITCMD(Do_InitSerial, "Init Serial");

#if 0
int Do_InitConfig(void)
{
    //TODO: Add your code here
    TOUT(0, 1, printf("Init Config\n"));
    return -1;
}
DECL_INITCMD(Do_InitConfig, "Config");
#endif




//==========================================================================
int Do_CheckKeyHold(void)
{
    return 0;
}

char *Xo_GetTargetBoard()
{
    //TODO: Add your code here
    return "c28";
}

extern "C" {
void hw_reset(void);
}
int Do_Quit(char *param)
{
    TOUT(1, 1, printf("Quit [%s]\n", param));
    hw_reset();
    return 0;
}

int Do_Reset(char *param)
{
    TOUT(1, 1, printf("Reset ..."););
    hw_reset();
    return 0;
}

extern "C" {
int UsbDownloadImageEx();
}
int Do_DownLoad(char *param)
{
    TOUT(1, 1, printf("DownLoad [%s]\n", param));
#define KERNEL_RAM_START		0xA0020000 //to avoid header definition collision
    unsigned long uImageBase = KERNEL_RAM_START;
    if (*param) param = Bo_ParseInteger(param, &uImageBase);

    char cmd[256];
    TOUT(1, 1, printf(cmd, "usb %08x", uImageBase));
    if (*param) Bo_ExeCommand(0, cmd);
    else {//old mode
        printk("Download with old mode!!!\n");
        UsbDownloadImageEx();
    }
    return 0;
}

extern "C" {
void BootKernel(int arg0, int arg1);
}
int Do_Jump(char *param)
{
    TOUT(1, 1, printf("jump kernel [%s]\n", param));
    if (*param) {
        unsigned long address;
        param = Bo_ParseInteger(param, &address);
        unsigned long arg0 = 0, arg1 = 0;
        if (*param) {
            param = Bo_ParseInteger(param, &arg0);
            if (*param) {
                param = Bo_ParseInteger(param, &arg1);
            }
        }
        void (*Func)(int, int)= (void(*)(int, int))address;

        TOUT(1, 1, printf("jump 0x%08x(%x, %x)\n", Func, arg0, arg1));
        (*Func)(arg0, arg1);
    }
    else {//old mode
        TOUT(1, 1, printf("Boot with old mode!!!\n"));
        BootKernel(0, 0);
    }

	TOUT(1, 1, printf("Hey, the kernel returned! This should not happen.\n"));
    return -1;
}

DECL_MAINCMD(Do_DownLoad, \
    "download - download os image",  \
    "...\n");

DECL_MAINCMD(Do_Quit, "quit   - quit with reset the machine",  \
    "...");

DECL_MAINCMD(Do_Reset, "reset - reset machine", "...");

DECL_MAINCMD(Do_Jump, \
    "jump kernel entrypoint",    \
    "...\n")
